var request = require('../../utils/request.js');
var util = require('../../utils/util.js');
var app = getApp();
Page({
  data: {
    curId: 1,
    // 页面中的数据
    list: [],
    length:0,
    showTip: false,
    winHeight: "",//窗口高度
    currentTab: 0, //预设当前项的值
    scrollLeft: 0, //tab标题的滚动条位置
    selectB:false,
    colorArr:[],
    sum:0,
    count:0,
    numb:0
  },
  // 滚动切换标签样式
  switchTab: function (e) {
    this.setData({
      currentTab: e.detail.current,
      curId: e.detail.current+1
    });
    this.checkCor();
    var that = this;
    that.getToken();
    that.total();
    
  },
  // 点击标题切换当前页时改变样式
  swichNav: function (e) {
    var cur = e.target.dataset.current;
    if (this.data.currentTaB == cur) { return false; }
    else {
      this.setData({
        currentTab: cur
      })
    }
    this.checkCor();
    var that = this;
    that.getToken();
    this.total();

  },
  //点击跳转到详情
  goBorrow: function (e) {
    console.log(getCurrentPages())
    var that = this;
    wx.navigateTo({
      url: 'Borrow-ing/Borrow-ing?id=' + e.currentTarget.dataset.id + "&curId=" + that.data.curId,
    })
  },
  //判断当前滚动超过一屏时，设置tab标题滚动条。
  checkCor: function () {
    if (this.data.currentTab > 4) {
      this.setData({         
        scrollLeft: 300
      })
    } else {
      this.setData({
        scrollLeft: 0
      })
    }
  },
  SelectFn:function(){
    var that = this;
    if(that.data.selectB){
      that.setData({
        selectB:false
      })
      this.colorCho(false)
    }else{
      that.setData({
        selectB: true
      })
      this.colorCho(true)
    }
    this.total()
  },
  // 多选的选择框点击事件；
  radioTap:function(e){
    // console.log(e.currentTarget.dataset.index);
    var index = e.currentTarget.dataset.index;
    var array = this.data.colorArr;
    var arr = [];
    // console.log(array);
    for(var i=0 ; i<array.length;i++){
      if(i == index){
         arr.push(!array[i])
      }else{
        arr.push(array[i])
      }
    }
    console.log(arr)

    this.setData({
      colorArr:arr
    })

    this.total()
  },
  total:function(){
      var that = this;
      // console.log(that.data.colorArr)
      var arr1 = that.data.colorArr;
      var list = that.data.list;
      // console.log(list)
      // 计算总体的待支付的价格
      var arr2 = [];
      for(var i=0 ; i<arr1.length ; i++){
         if(arr1[i]){
            // console.log(i);
            arr2.push(list[i]);
         }
      }

      var sum = 0;
      var count = arr2.length;
      if (count == list.length){
        that.setData({
          selectB:true
        })
      }else{
        that.setData({
          selectB: false
        })
      }
      for(var j=0 ; j<arr2.length ; j++){
        sum = arr2[j].money;
      }

      that.setData({
          sum:sum,
          count:count
      })
  },
  onLoad: function () {
    var that = this;
    //  高度自适应
    wx.getSystemInfo({
      success: function (res) {
        var clientHeight = res.windowHeight,
          clientWidth = res.windowWidth,
          rpxR = 750 / clientWidth;
        var calc = clientHeight * rpxR;
        console.log(calc)
        that.setData({
          winHeight: calc
        });
      }
    });

  },
  onShow:function(){
    var that = this;
    that.getToken();
  },
  getToken: function () {
    console.log(getCurrentPages())
    var that = this;
    // 获取本地存储中的token
    wx.getStorage({
      key: 'token',
      success: function (res) {//有该数据
        console.log(res.data)
        that.setData({
          token: res.data
        })
        var tempToken = res.data;
        console.log({
          "session_id": tempToken,
          "is_pay": that.data.curId
        })
        request.getOrderList(
          {
            "session_id": tempToken,
            "is_pay": that.data.curId
          },
          (res) => {
            console.log(res);
            if (res.data.status == "success") {
              if (res.data.code == "1") {
                that.setData({
                  list: res.data.data,
                  length: res.data.data.length,
                  showTip: false
                })
                // console.log(that.data.list)
                // console.log(res.data.data.length);
                that.colorCho(false)
                for (var i = 0; i < res.data.data.length; i++) {
                  if (that.data.list[i].l_adress) {
                    that.data.list[i].l_adress = util.cutTitleString(res.data.data[i].l_adress, 0, 12)
                  }
                  if (that.data.list[i].name) {

                    that.data.list[i].name = util.cutTitleString(res.data.data[i].name, 0, 12)
                  }
                  if (that.data.list[i].order_no) {
                    that.data.list[i].order_no = util.cutTitleString(res.data.data[i].order_no, 0, 15)

                  }
                }
                that.setData({
                  list: that.data.list
                })
                if (that.data.curId == 1) {
                  console.log()
                  that.setData({
                    numb: that.data.list.length
                  })
                }

              }
              else if (res.data.code == "10002") {
                that.setData({
                  showTip: true
                })


              }
            } else {
              wx.showToast({
                title: '请求失败',
                image: "../../../images/xx.png"
              })
            }
            wx.hideLoading();
          }
        )



      },
      fail: function (res) {
        console.log(res.data)
        //调用授权方法-设置token
        app.getUserInfo();
      }
    })
  },
  // 跳转到详情
  goOrderDesc: function (e) {
    console.log(getCurrentPages())
    var that = this;
    wx.navigateTo({
      url: '../my-orderDesc/my-orderDesc?id=' + e.currentTarget.dataset.id + "&curId=" + that.data.curId,
    })
  },
  colorCho:function(item){
    var arr = [];
    // console.log(this.data.list)
    for (var i = 0; i < this.data.list.length; i++) {
      arr.push(item)
    }
    // console.log(arr)
    this.setData({
      colorArr: arr
    })
  },
  onUnload:function(){
    // console.log(this.data.list)
  },
  footerTap: app.footerTap
})